package cn.bonoon.kernel.web.html.form;

import cn.bonoon.handler.impl.StandardAutoManager;
import cn.bonoon.kernel.IdGenerator;
import cn.bonoon.kernel.descriptors.UIClassDescriptor;
import cn.bonoon.kernel.web.annotations.form.FormEditor;
import cn.bonoon.kernel.web.annotations.form.HasFile;

public abstract class AbstractEditableBuilder extends AbstractFormBuilder implements EditableParser{
	private boolean hasFile;
	private final String formId;
	protected AbstractEditableBuilder(UIClassDescriptor objectDescriptor, StandardAutoManager manager) {
		super(objectDescriptor, manager);
		this.formId = IdGenerator.fixed();
	}
	
	@Override
	protected void init() {
		super.init();
		FormEditor fd = uiDescriptor.getAnnotation(FormEditor.class);
		if(null != fd){
			if(fd.value() > 0){
				column= fd.value();
			}
			if(fd.headWidth() > 0){
				headWidth = fd.headWidth();
			}
			if(fd.width() > 0){
				propertyWidth = fd.width();
			}
			if(!fd.formStyleClass().isEmpty()){
				tableClass = fd.formStyleClass();
			}
		}
		
		hasFile = uiDescriptor.getAnnotation(HasFile.class) != null;
	}

	public void hasFile(){
		hasFile = true;
	}

	@Override
	protected void __parseForm(StringBuilder sb) {
		sb.append("<form method='post' id='").append(formId);
		if (hasFile){
			sb.append("' enctype='multipart/form-data");
		}
		sb.append("' style='padding:0;margin:0;'>");
		super.__parseForm(sb);
		sb.append("</form>");
	}

	public String getFormId() {
		return formId;
	}
}
